interface objSwitchType {
  arr: string[];
  obj: any;
}

export default class Const {
  static passwordReg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,15}$/;
  static successCode = 200;
  static paramsSwitch: objSwitchType = {
    arr: ['current'],
    obj: {
      current: 'pageNumber',
    },
  };
  static resDataSwitch: objSwitchType = {
    arr: ['totalRecords', 'currentPage'],
    obj: {
      totalRecords: 'total',
      currentPage: 'current',
    },
  };
  // 处理请求参数
  static dealParamsSwitch = (params: any) => {
    let obj: any = {};
    for (let key in params) {
      if (Const.paramsSwitch.arr.indexOf(key) > -1) {
        obj[Const.paramsSwitch.obj[key]] = params[key] || '';
      } else {
        obj[key] = params[key] || '';
      }
    }
    return obj;
  };
  // 处理返回data
  static dealResDataSwitch = (res: any) => {
    let resData: any = {};
    for (let key in res) {
      if (Const.resDataSwitch.arr.indexOf(key) > -1) {
        resData[Const.resDataSwitch.obj[key]] = res[key] || '';
      } else {
        resData[key] = res[key] || '';
      }
    }
    return resData;
  };
  // 日期格式
  static format = 'YYYY-MM-DD';
  // 处理成valueEnum
  static dealMap = (arr: any) => {
    let obj: any = {};
    for (let item of arr) {
      obj[item.value] = {
        text: item.label,
      };
    }
    return obj;
  };
  // 客户状态
  static clienteleStatusList = [
    { label: '全部', value: '' },
    { label: '停用', value: '0' },
    { label: '启用', value: '1' },
    { label: '过期', value: '3' },
    { label: '未启用', value: '4' },
  ];
  static clienteleStatusMap = Const.dealMap(Const.clienteleStatusList);
  static requestUrlMap: any = {
    '/api/sys/tenant/page': '客户列表',
    '/api/sys/tenant/change_status': '启用停用客户',
    '/api/sys/tenant/save': '新增客户',
    '/api/sys/tenant/update': '编辑客户',
    '/api/sys/tenant/get/*': '客户详情',
    '/api/common/menu/get_by_scope': '菜单资源树',
    '/api/uc/user/ent_admin': '获取管理员用户名',
  };
  // 获取存储操作日志记录的对象数据
  static getLogParams = (url: string) => {
    let breadcrumbDataArr: any =
      JSON.parse(<any>sessionStorage.getItem('breadcrumbDataArr')) || [];
    let menu = breadcrumbDataArr.map((item: any) => item.name).join('-');
    let userDetail = JSON.parse(<any>localStorage.getItem('userDetail')) || {};
    const permissionName: string = Const.requestUrlMap[url] || '';
    let obj = {
      menu: encodeURI(menu), // 所属菜单
      operator: encodeURI(userDetail.tuCname || ''), // 操作人
      operation: encodeURI(
        breadcrumbDataArr[0]?.name +
          (permissionName ? '-' + permissionName : ''),
      ), // 操作内容
    };
    return obj;
  };
}
